import {messages} from "share/common";
import React, { Component } from 'react'
import { Modal, message } from 'antd';

const CheckModal = WrappedComponent => {
  class hoc extends Component{
    constructor(props){
      super(props);
      this.state = {

      }
    }

    checkBrowser = seqNo => {
      let versionNumber = [9, 10, 11];
      if (versionNumber.indexOf(this.IEVersion()) > -1) {
        return this.checkAgent(seqNo);
      } else {
        Modal.error({
          title: messages('pay.key226')/*浏览器不兼容*/,
          content: messages('pay.key227')/*当前浏览器不兼容，请选择IE*/
        });
        return null
      }
    };

    IEVersion = () => {
      let userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
      let isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //判断是否IE<11浏览器
      let isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器
      let isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf("rv:11.0") > -1;
      if(isIE) {
        let reIE = new RegExp("MSIE (\\d+\\.\\d+);");
        reIE.test(userAgent);
        let fIEVersion = parseFloat(RegExp["$1"]);
        if (fIEVersion > 6) {// IE版本：7,8,9,10
          return fIEVersion
        } else { //IE版本<=6
          return 6;
        }
      } else if(isEdge) {
        return 'edge';//edge
      } else if(isIE11) {
        return 11; //IE11
      }else{
        return -1;//不是ie浏览器
      }
    };

    checkAgent = seqNo => {
      let eDiv = document.createElement("div");
      let link = '';
      if (window.navigator.cpuClass == "x86") {
        eDiv.innerHTML = "<object id=\"CryptoAgent\" codebase=\"CryptoKit.Ultimate.x86.cab\" classid=\"clsid:4C588282-7792-4E16-93CB-9744402E4E98\" ></object>";
        link = `https://cloudhelios-static.oss-cn-shanghai.aliyuncs.com/thirdparty2019/CryptoKit.Ultimate.x86.exe`;
      }
      else {
        eDiv.innerHTML = "<object id=\"CryptoAgent\" codebase=\"CryptoKit.Ultimate.x64.cab\" classid=\"clsid:B2F2D4D4-D808-43B3-B355-B671C0DE15D4\" ></object>";
        link = `https://cloudhelios-static.oss-cn-shanghai.aliyuncs.com/thirdparty2019/CryptoKit.Ultimate.x64.exe`;
      }
      document.body.appendChild(eDiv);
      let CryptoAgent = document.getElementById("CryptoAgent");
      let isContinue = false;
      try{
        CryptoAgent.SetSM2CSPList('CFCA CSP v3.0||CFCA Ulan CSP v4.0');
        isContinue = true;
      } catch (e) {
        console.log(e);
        Modal.error({
          title: messages('pay.key228')/*请先安装UKey插件*/,
          content: messages('pay.key229')/*请点击下方【去下载】按钮，跳转下载并安装，安装后请刷新本页面重新操作。*/,
          onOk: () => {
            window.open(link, '_blank');
          },
          okText: messages('pay.key230')/*去下载*/
        })
      }
      let result = null;
      isContinue && (result = this.checkUkey(CryptoAgent, seqNo));
      return result;
    };

    checkUkey = (CryptoAgent, seqNo) => {
      try {
        CryptoAgent.SelectCertificate('','CFCA','');
        let certInfo = CryptoAgent.GetSignCertInfo('SubjectCN');
        let source = seqNo + certInfo.split('@')[1];
        let res = CryptoAgent.SignMsgPKCS7(source, 'SHA-256', false);
        return {certInfo, res};
      } catch (e) {
        let errorDesc = CryptoAgent.GetLastErrorDesc();
        message.error(errorDesc);
        return null
      }
    };

    orderTab = data => {
      let tabMap = ['ONLINE_PAYMENT', 'OFFLINE_PAYMENT', 'EBANK_PAYMENT'];
      data.sort((a,b) => {
        return tabMap.indexOf(a.itemCode) - tabMap.indexOf(b.itemCode);
      });
      return data;
    };

    render(){
      return(
        <WrappedComponent
          checkBrowser={this.checkBrowser}
          orderTab={this.orderTab}
          {...this.props}
        />
      )
    }
  }

  return hoc;
};

export default CheckModal;
